<!DOCTYPE html><html lang="zh-CN" data-theme="light"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width,initial-scale=1"><title>PHP7 新特性与变更 | 指尖改变世界</title><meta name="keywords" content="PHP"><meta name="author" content="沙屿沫"><meta name="copyright" content="沙屿沫"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#ffffff"><meta name="description" content="官方文档： 从PHP 5.6.x 移植到 PHP 7.0.x PHP官方文档上面说到, PHP7 相对于 PHP5.6 来说，是一个大版本的改变。 现在我们整理一下可能会影响到日常开发的点（注：实际变更涉及到的点不仅限于下述几点）。 【不向后兼容变更】异常处理和错误的更改在PHP 7中，很多致命错误以及可恢复的致命错误，都被转换为异常来处理了。 这些异常继承自Error类，此类实现了Throwab">
<meta property="og:type" content="article">
<meta property="og:title" content="PHP7 新特性与变更">
<meta property="og:url" content="http://blog.shayvmo.cn/2020/06/12/php7/index.html">
<meta property="og:site_name" content="指尖改变世界">
<meta property="og:description" content="官方文档： 从PHP 5.6.x 移植到 PHP 7.0.x PHP官方文档上面说到, PHP7 相对于 PHP5.6 来说，是一个大版本的改变。 现在我们整理一下可能会影响到日常开发的点（注：实际变更涉及到的点不仅限于下述几点）。 【不向后兼容变更】异常处理和错误的更改在PHP 7中，很多致命错误以及可恢复的致命错误，都被转换为异常来处理了。 这些异常继承自Error类，此类实现了Throwab">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="http://blog.shayvmo.cn/images/php.png">
<meta property="article:published_time" content="2020-06-12T06:42:57.000Z">
<meta property="article:modified_time" content="2022-08-23T09:51:28.576Z">
<meta property="article:author" content="沙屿沫">
<meta property="article:tag" content="PHP">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="http://blog.shayvmo.cn/images/php.png"><link rel="shortcut icon" href="/images/avatar.jpg"><link rel="canonical" href="http://blog.shayvmo.cn/2020/06/12/php7/"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="preconnect" href="//hm.baidu.com"/><link rel="preconnect" href="//busuanzi.ibruce.info"/><link rel="stylesheet" href="/css/index.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free/css/all.min.css" media="print" onload="this.media='all'"><script>var _hmt = _hmt || [];
(function() {
  var hm = document.createElement("script");
  hm.src = "https://hm.baidu.com/hm.js?5e4d1e8fcc524b4e732ea58fa9fa15f0";
  var s = document.getElementsByTagName("script")[0]; 
  s.parentNode.insertBefore(hm, s);
})();
</script><script>const GLOBAL_CONFIG = { 
  root: '/',
  algolia: undefined,
  localSearch: {"path":"search.xml","languages":{"hits_empty":"找不到您查询的内容：${query}"}},
  translate: undefined,
  noticeOutdate: undefined,
  highlight: {"plugin":"highlighjs","highlightCopy":true,"highlightLang":true,"highlightHeightLimit":false},
  copy: {
    success: '复制成功',
    error: '复制错误',
    noSupport: '浏览器不支持'
  },
  relativeDate: {
    homepage: false,
    post: false
  },
  runtime: '天',
  date_suffix: {
    just: '刚刚',
    min: '分钟前',
    hour: '小时前',
    day: '天前',
    month: '个月前'
  },
  copyright: undefined,
  lightbox: 'fancybox',
  Snackbar: undefined,
  source: {
    jQuery: 'https://cdn.jsdelivr.net/npm/jquery@latest/dist/jquery.min.js',
    justifiedGallery: {
      js: 'https://cdn.jsdelivr.net/npm/justifiedGallery/dist/js/jquery.justifiedGallery.min.js',
      css: 'https://cdn.jsdelivr.net/npm/justifiedGallery/dist/css/justifiedGallery.min.css'
    },
    fancybox: {
      js: 'https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@latest/dist/jquery.fancybox.min.js',
      css: 'https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@latest/dist/jquery.fancybox.min.css'
    }
  },
  isPhotoFigcaption: false,
  islazyload: true,
  isanchor: false
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = {
  title: 'PHP7 新特性与变更',
  isPost: true,
  isHome: false,
  isHighlightShrink: false,
  isToc: true,
  postUpdate: '2022-08-23 17:51:28'
}</script><noscript><style type="text/css">
  #nav {
    opacity: 1
  }
  .justified-gallery img {
    opacity: 1
  }

  #recent-posts time,
  #post-meta time {
    display: inline !important
  }
</style></noscript><script>(win=>{
    win.saveToLocal = {
      set: function setWithExpiry(key, value, ttl) {
        if (ttl === 0) return
        const now = new Date()
        const expiryDay = ttl * 86400000
        const item = {
          value: value,
          expiry: now.getTime() + expiryDay,
        }
        localStorage.setItem(key, JSON.stringify(item))
      },

      get: function getWithExpiry(key) {
        const itemStr = localStorage.getItem(key)

        if (!itemStr) {
          return undefined
        }
        const item = JSON.parse(itemStr)
        const now = new Date()

        if (now.getTime() > item.expiry) {
          localStorage.removeItem(key)
          return undefined
        }
        return item.value
      }
    }
  
    win.getScript = url => new Promise((resolve, reject) => {
      const script = document.createElement('script')
      script.src = url
      script.async = true
      script.onerror = reject
      script.onload = script.onreadystatechange = function() {
        const loadState = this.readyState
        if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
        script.onload = script.onreadystatechange = null
        resolve()
      }
      document.head.appendChild(script)
    })
  
      win.activateDarkMode = function () {
        document.documentElement.setAttribute('data-theme', 'dark')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
        }
      }
      win.activateLightMode = function () {
        document.documentElement.setAttribute('data-theme', 'light')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#ffffff')
        }
      }
      const t = saveToLocal.get('theme')
    
          if (t === 'dark') activateDarkMode()
          else if (t === 'light') activateLightMode()
        
      const asideStatus = saveToLocal.get('aside-status')
      if (asideStatus !== undefined) {
        if (asideStatus === 'hide') {
          document.documentElement.classList.add('hide-aside')
        } else {
          document.documentElement.classList.remove('hide-aside')
        }
      }
    })(window)</script><meta name="generator" content="Hexo 5.4.0"></head><body><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="author-avatar"><img class="avatar-img" src= "" data-lazy-src="/images/avatar.jpg" onerror="onerror=null;src='/img/friend_404.gif'" alt="avatar"/></div><div class="site-data"><div class="data-item is-center"><div class="data-item-link"><a href="/archives/"><div class="headline">文章</div><div class="length-num">74</div></a></div></div><div class="data-item is-center"><div class="data-item-link"><a href="/tags/"><div class="headline">标签</div><div class="length-num">42</div></a></div></div><div class="data-item is-center"><div class="data-item-link"><a href="/categories/"><div class="headline">分类</div><div class="length-num">15</div></a></div></div></div><hr/><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="/archives/"><i class="fa-fw fas fa-archive"></i><span> 时间轴</span></a></div><div class="menus_item"><a class="site-page" href="/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page" href="/link/"><i class="fa-fw fas fa-link"></i><span> 友情链接</span></a></div><div class="menus_item"><a class="site-page" href="/about/"><i class="fa-fw fas fa-heart"></i><span> 关于</span></a></div></div></div></div><div class="post" id="body-wrap"><header class="post-bg" id="page-header" style="background-image: url('/images/php.png')"><nav id="nav"><span id="blog_name"><a id="site-name" href="/">指尖改变世界</a></span><div id="menus"><div id="search-button"><a class="site-page social-icon search"><i class="fas fa-search fa-fw"></i><span> 搜索</span></a></div><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="/archives/"><i class="fa-fw fas fa-archive"></i><span> 时间轴</span></a></div><div class="menus_item"><a class="site-page" href="/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page" href="/link/"><i class="fa-fw fas fa-link"></i><span> 友情链接</span></a></div><div class="menus_item"><a class="site-page" href="/about/"><i class="fa-fw fas fa-heart"></i><span> 关于</span></a></div></div><div id="toggle-menu"><a class="site-page"><i class="fas fa-bars fa-fw"></i></a></div></div></nav><div id="post-info"><h1 class="post-title">PHP7 新特性与变更</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">发表于</span><time class="post-meta-date-created" datetime="2020-06-12T06:42:57.000Z" title="发表于 2020-06-12 14:42:57">2020-06-12</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">更新于</span><time class="post-meta-date-updated" datetime="2022-08-23T09:51:28.576Z" title="更新于 2022-08-23 17:51:28">2022-08-23</time></span><span class="post-meta-categories"><span class="post-meta-separator">|</span><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/categories/PHP/">PHP</a></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-wordcount"><i class="far fa-file-word fa-fw post-meta-icon"></i><span class="post-meta-label">字数总计:</span><span class="word-count">1.3k</span><span class="post-meta-separator">|</span><i class="far fa-clock fa-fw post-meta-icon"></i><span class="post-meta-label">阅读时长:</span><span>5分钟</span></span><span class="post-meta-separator">|</span><span class="post-meta-pv-cv" id="" data-flag-title="PHP7 新特性与变更"><i class="far fa-eye fa-fw post-meta-icon"></i><span class="post-meta-label">阅读量:</span><span id="busuanzi_value_page_pv"></span></span></div></div></div></header><main class="layout" id="content-inner"><div id="post"><article class="post-content" id="article-container"><p>官方文档： <a target="_blank" rel="noopener" href="https://www.php.net/manual/zh/migration70.php">从PHP 5.6.x 移植到 PHP 7.0.x</a></p>
<p><code>PHP</code>官方文档上面说到, <code>PHP7</code> 相对于 <code>PHP5.6</code> 来说，是一个大版本的改变。</p>
<p>现在我们整理一下可能会影响到日常开发的点（注：实际变更涉及到的点不仅限于下述几点）。</p>
<h3 id="【不向后兼容变更】"><a href="#【不向后兼容变更】" class="headerlink" title="【不向后兼容变更】"></a>【不向后兼容变更】</h3><h4 id="异常处理和错误的更改"><a href="#异常处理和错误的更改" class="headerlink" title="异常处理和错误的更改"></a>异常处理和错误的更改</h4><p>在<code>PHP 7</code>中，很多致命错误以及可恢复的致命错误，都被转换为异常来处理了。 这些异常继承自<code>Error</code>类，此类实现了<code>Throwable</code>接口 （所有异常都实现了这个基础接口）。</p>
<p>这也意味着，当发生错误的时候，以前代码中的一些错误处理的代码将无法被触发。 因为在<code>PHP 7</code>版本中，已经使用抛出异常的错误处理机制了。 （如果代码中没有捕获<code>Error</code>异常，那么会引发致命错误）。</p>
<h4 id="关于间接使用变量、属性和方法的变化"><a href="#关于间接使用变量、属性和方法的变化" class="headerlink" title="关于间接使用变量、属性和方法的变化"></a>关于间接使用变量、属性和方法的变化</h4><p>对变量、属性和方法的间接调用现在将<strong><code>严格遵循从左到右的顺序</code></strong>来解析</p>
<p>使用了旧的从右到左的解析顺序的代码必须被重写，明确的使用<strong><code>大括号</code></strong>来表明顺序（参见示例 <code>PHP5</code> 的写法）。 这样使得代码既保持了与PHP 7.x的前向兼容性，又保持了与PHP 5.x的后向兼容性。</p>
<p>示例：</p>
<figure class="highlight php"><table><tr><td class="code"><pre><span class="line"></span><br><span class="line"><span class="variable">$a</span> = <span class="string">&#x27;b&#x27;</span>;</span><br><span class="line"></span><br><span class="line"><span class="variable">$b</span> = [</span><br><span class="line">    <span class="string">&#x27;a&#x27;</span>=&gt;[</span><br><span class="line">        <span class="string">&#x27;aa&#x27;</span>=&gt;<span class="string">&#x27;a&#x27;</span>,</span><br><span class="line">    ],</span><br><span class="line">];</span><br><span class="line"></span><br><span class="line"><span class="keyword">echo</span> <span class="variable">$$b</span>[<span class="string">&#x27;a&#x27;</span>][<span class="string">&#x27;aa&#x27;</span>]; <span class="comment"># echo b in php 5</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">echo</span> <span class="variable">$$b</span>[<span class="string">&#x27;a&#x27;</span>][<span class="string">&#x27;aa&#x27;</span>]; <span class="comment"># php7 中则会解析成 ($$b)[&#x27;a&#x27;][&#x27;aa&#x27;]，如果要想达到php5中的输出结果，则需要加上大括号   $&#123;$b[&#x27;a&#x27;][&#x27;aa&#x27;]&#125;</span></span><br><span class="line"></span><br></pre></td></tr></table></figure>


<h4 id="list-处理方式变动"><a href="#list-处理方式变动" class="headerlink" title="list() 处理方式变动"></a>list() 处理方式变动</h4><h4 id="foreach-变化"><a href="#foreach-变化" class="headerlink" title="foreach 变化"></a>foreach 变化</h4><p>(1) foreach 不再改变内部数组指针</p>
<figure class="highlight php"><table><tr><td class="code"><pre><span class="line"><span class="variable">$array</span> = [<span class="number">0</span>, <span class="number">1</span>, <span class="number">2</span>];</span><br><span class="line"><span class="keyword">foreach</span> (<span class="variable">$array</span> <span class="keyword">as</span> &amp;<span class="variable">$val</span>) &#123;</span><br><span class="line">    var_dump(current(<span class="variable">$array</span>));</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment"># in php 5</span></span><br><span class="line"><span class="keyword">int</span>(<span class="number">1</span>)</span><br><span class="line"><span class="keyword">int</span>(<span class="number">2</span>)</span><br><span class="line"><span class="keyword">bool</span>(<span class="literal">false</span>)</span><br><span class="line"></span><br><span class="line"><span class="comment"># in php 7</span></span><br><span class="line"><span class="keyword">int</span>(<span class="number">0</span>)</span><br><span class="line"><span class="keyword">int</span>(<span class="number">0</span>)</span><br><span class="line"><span class="keyword">int</span>(<span class="number">0</span>)</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<p>(2) 通过值遍历时，操作的值为数组的副本</p>
<p>(3) 通过引用遍历时，有更好的迭代特性</p>
<h4 id="HTTP-RAW-POST-DATA-被移除"><a href="#HTTP-RAW-POST-DATA-被移除" class="headerlink" title="$HTTP_RAW_POST_DATA 被移除"></a>$HTTP_RAW_POST_DATA 被移除</h4><p>不再提供<code>$HTTP_RAW_POST_DATA</code>变量。 请使用<code>php://input</code>作为替代。</p>
<h3 id="【新特性】"><a href="#【新特性】" class="headerlink" title="【新特性】"></a>【新特性】</h3><h4 id="标量类型声明"><a href="#标量类型声明" class="headerlink" title="标量类型声明"></a>标量类型声明</h4><figure class="highlight php"><table><tr><td class="code"><pre><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">sumOfInts</span>(<span class="params"><span class="keyword">int</span> ...<span class="variable">$ints</span></span>)</span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="keyword">return</span> array_sum(<span class="variable">$ints</span>);</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment"># 输出 int(9)</span></span><br><span class="line">var_dump(sumOfInts(<span class="number">2</span>, <span class="string">&#x27;3&#x27;</span>, <span class="number">4.1</span>));</span><br><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h4 id="返回值类型声明"><a href="#返回值类型声明" class="headerlink" title="返回值类型声明"></a>返回值类型声明</h4><figure class="highlight php"><table><tr><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">arraysSum</span>(<span class="params"><span class="keyword">array</span> ...<span class="variable">$arrays</span></span>): <span class="title">array</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="keyword">return</span> array_map(<span class="function"><span class="keyword">function</span>(<span class="params"><span class="keyword">array</span> <span class="variable">$array</span></span>): <span class="title">int</span> </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> array_sum(<span class="variable">$array</span>);</span><br><span class="line">    &#125;, <span class="variable">$arrays</span>);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">print_r(arraysSum([<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>], [<span class="number">4</span>,<span class="number">5</span>,<span class="number">6</span>], [<span class="number">7</span>,<span class="number">8</span>,<span class="number">9</span>]));</span><br><span class="line"></span><br><span class="line"><span class="comment"># 输出一个数组</span></span><br><span class="line"><span class="keyword">Array</span></span><br><span class="line">(</span><br><span class="line">    [<span class="number">0</span>] =&gt; <span class="number">6</span></span><br><span class="line">    [<span class="number">1</span>] =&gt; <span class="number">15</span></span><br><span class="line">    [<span class="number">2</span>] =&gt; <span class="number">24</span></span><br><span class="line">)</span><br></pre></td></tr></table></figure>

<h4 id="null合并运算符"><a href="#null合并运算符" class="headerlink" title="null合并运算符"></a><code>null</code>合并运算符</h4><p>由于日常使用中存在大量同时使用三元表达式和 isset()的情况， 我们添加了null合并运算符 (??) 这个语法糖。</p>
<p>如果变量存在且值不为NULL， 它就会返回自身的值，否则返回它的第二个操作数。</p>
<figure class="highlight php"><table><tr><td class="code"><pre><span class="line"></span><br><span class="line"><span class="variable">$username</span> = <span class="variable">$_GET</span>[<span class="string">&#x27;user&#x27;</span>] ?? <span class="string">&#x27;nobody&#x27;</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment"># 等同于</span></span><br><span class="line"><span class="variable">$username</span> = <span class="keyword">isset</span>(<span class="variable">$_GET</span>[<span class="string">&#x27;user&#x27;</span>]) ? <span class="variable">$_GET</span>[<span class="string">&#x27;user&#x27;</span>] : <span class="string">&#x27;nobody&#x27;</span>;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<p>拓展</p>
<figure class="highlight php"><table><tr><td class="code"><pre><span class="line"><span class="variable">$a</span> = <span class="string">&#x27;&#x27;</span>; <span class="comment">// or 0 or false</span></span><br><span class="line"></span><br><span class="line"><span class="variable">$b</span> = <span class="variable">$a</span> ?? <span class="string">&#x27;a&#x27;</span>; <span class="comment">// $b is &#x27;&#x27; or 0 or false</span></span><br><span class="line"></span><br><span class="line"><span class="variable">$c</span> = <span class="variable">$a</span> ?: <span class="string">&#x27;a&#x27;</span>;<span class="comment">// $c is &#x27;a&#x27;</span></span><br></pre></td></tr></table></figure>



<h4 id="太空船操作符（组合比较符）-lt-gt"><a href="#太空船操作符（组合比较符）-lt-gt" class="headerlink" title="太空船操作符（组合比较符） &lt;=&gt;"></a>太空船操作符（组合比较符） <code>&lt;=&gt;</code></h4><p>太空船操作符用于比较两个表达式。当$a小于、等于或大于$b时它分别返回-1、0或1。 比较的原则是沿用 PHP 的常规比较规则进行的。</p>
<figure class="highlight php"><table><tr><td class="code"><pre><span class="line"><span class="comment">// 整数</span></span><br><span class="line"><span class="keyword">echo</span> <span class="number">1</span> &lt;=&gt; <span class="number">1</span>; <span class="comment">// 0</span></span><br><span class="line"><span class="keyword">echo</span> <span class="number">1</span> &lt;=&gt; <span class="number">2</span>; <span class="comment">// -1</span></span><br><span class="line"><span class="keyword">echo</span> <span class="number">2</span> &lt;=&gt; <span class="number">1</span>; <span class="comment">// 1</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// 浮点数</span></span><br><span class="line"><span class="keyword">echo</span> <span class="number">1.5</span> &lt;=&gt; <span class="number">1.5</span>; <span class="comment">// 0</span></span><br><span class="line"><span class="keyword">echo</span> <span class="number">1.5</span> &lt;=&gt; <span class="number">2.5</span>; <span class="comment">// -1</span></span><br><span class="line"><span class="keyword">echo</span> <span class="number">2.5</span> &lt;=&gt; <span class="number">1.5</span>; <span class="comment">// 1</span></span><br><span class="line"> </span><br><span class="line"><span class="comment">// 字符串</span></span><br><span class="line"><span class="keyword">echo</span> <span class="string">&quot;a&quot;</span> &lt;=&gt; <span class="string">&quot;a&quot;</span>; <span class="comment">// 0</span></span><br><span class="line"><span class="keyword">echo</span> <span class="string">&quot;a&quot;</span> &lt;=&gt; <span class="string">&quot;b&quot;</span>; <span class="comment">// -1</span></span><br><span class="line"><span class="keyword">echo</span> <span class="string">&quot;b&quot;</span> &lt;=&gt; <span class="string">&quot;a&quot;</span>; <span class="comment">// 1</span></span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h4 id="通过define-定义常量数组"><a href="#通过define-定义常量数组" class="headerlink" title="通过define()定义常量数组"></a>通过<code>define()</code>定义常量数组</h4><figure class="highlight php"><table><tr><td class="code"><pre><span class="line">define(<span class="string">&#x27;ANIMALS&#x27;</span>, [</span><br><span class="line">    <span class="string">&#x27;dog&#x27;</span>,</span><br><span class="line">    <span class="string">&#x27;cat&#x27;</span>,</span><br><span class="line">    <span class="string">&#x27;bird&#x27;</span></span><br><span class="line">]);</span><br><span class="line"></span><br><span class="line"><span class="keyword">echo</span> ANIMALS[<span class="number">1</span>]; <span class="comment">// 输出 &quot;cat&quot;</span></span><br></pre></td></tr></table></figure>

<h4 id="匿名类"><a href="#匿名类" class="headerlink" title="匿名类"></a>匿名类</h4><p>现在支持通过new class 来实例化一个匿名类，这可以用来替代一些“用后即焚”的完整类定义。</p>
<figure class="highlight php"><table><tr><td class="code"><pre><span class="line"><span class="class"><span class="keyword">interface</span> <span class="title">Logger</span> </span>&#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="function"><span class="keyword">function</span> <span class="title">log</span>(<span class="params"><span class="keyword">string</span> <span class="variable">$msg</span></span>)</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Application</span> </span>&#123;</span><br><span class="line">    <span class="keyword">private</span> <span class="variable">$logger</span>;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> <span class="function"><span class="keyword">function</span> <span class="title">getLogger</span>(<span class="params"></span>): <span class="title">Logger</span> </span>&#123;</span><br><span class="line">         <span class="keyword">return</span> <span class="keyword">$this</span>-&gt;logger;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> <span class="function"><span class="keyword">function</span> <span class="title">setLogger</span>(<span class="params">Logger <span class="variable">$logger</span></span>) </span>&#123;</span><br><span class="line">         <span class="keyword">$this</span>-&gt;logger = <span class="variable">$logger</span>;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="variable">$app</span> = <span class="keyword">new</span> Application;</span><br><span class="line"><span class="variable">$app</span>-&gt;setLogger(<span class="keyword">new</span> <span class="class"><span class="keyword">class</span> <span class="keyword">implements</span> <span class="title">Logger</span> </span>&#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="function"><span class="keyword">function</span> <span class="title">log</span>(<span class="params"><span class="keyword">string</span> <span class="variable">$msg</span></span>) </span>&#123;</span><br><span class="line">        <span class="keyword">echo</span> <span class="variable">$msg</span>;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;);</span><br><span class="line"></span><br><span class="line">var_dump(<span class="variable">$app</span>-&gt;getLogger());</span><br><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h4 id="整数除法函数intdiv"><a href="#整数除法函数intdiv" class="headerlink" title="整数除法函数intdiv()"></a>整数除法函数<code>intdiv()</code></h4><p>新加的函数 intdiv() 用来进行 整数的除法运算。</p>
<figure class="highlight php"><table><tr><td class="code"><pre><span class="line"><span class="comment"># 输出 int(3)</span></span><br><span class="line">var_dump(intdiv(<span class="number">10</span>, <span class="number">3</span>)); </span><br></pre></td></tr></table></figure>

<h3 id="【PHP-7-0-x-弃用的功能】"><a href="#【PHP-7-0-x-弃用的功能】" class="headerlink" title="【PHP 7.0.x 弃用的功能】"></a>【PHP 7.0.x 弃用的功能】</h3><h4 id="password-hash-盐值选项"><a href="#password-hash-盐值选项" class="headerlink" title="password_hash() 盐值选项"></a>password_hash() 盐值选项</h4><p>废弃了 password_hash() 函数中的盐值选项，阻止开发者生成自己的盐值（通常更不安全）。 </p>
<p>开发者不传该值时，该函数自己会生成密码学安全的盐值。因此再无必要传入自己自定义的盐值。</p>
<h4 id="变更的函数"><a href="#变更的函数" class="headerlink" title="变更的函数"></a>变更的函数</h4><p><code>debug_zval_dump()</code> 现在打印 “int” 替代 “long”, 打印 “float” 替代 “double”</p>
<p><code>dirname()</code> 增加了可选的第二个参数, depth, 获取当前目录向上 depth 级父目录的名称。</p>
<p><code>getrusage()</code> 现在支持 Windows.</p>
<p><code>mktime()</code> and <code>gmmktime()</code> 函数不再接受 is_dst 参数。</p>
<p><code>preg_replace()</code> 函数不再支持 “\e” (PREG_REPLACE_EVAL). 应当使用 <code>preg_replace_callback()</code> 替代。</p>
<p><code>setlocale()</code> 函数不再接受 category 传入字符串。 应当使用 LC_* 常量。</p>
<p><code>exec()</code>, <code>system()</code> and <code>passthru()</code> 函数对 <code>NULL</code> 增加了保护.</p>
<p><code>shmop_open()</code> 现在返回一个资源而非一个int， 这个资源可以传给<code>shmop_size()</code>, <code>shmop_write()</code>, <code>shmop_read()</code>, <code>shmop_close()</code> 和 <code>shmop_delete()</code>.</p>
<p><code>substr()</code> 现在当 start 的值与 string 的长度相同时将返回一个空字符串。</p>
<p>为了避免内存泄露，<code>xml_set_object()</code> 现在在执行结束时需要手动清除 $parse。</p>
</article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta">文章作者: </span><span class="post-copyright-info"><a href="mailto:undefined">沙屿沫</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta">文章链接: </span><span class="post-copyright-info"><a href="http://blog.shayvmo.cn/2020/06/12/php7/">http://blog.shayvmo.cn/2020/06/12/php7/</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta">版权声明: </span><span class="post-copyright-info">本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank">CC BY-NC-SA 4.0</a> 许可协议。转载请注明来自 <a href="http://blog.shayvmo.cn" target="_blank">指尖改变世界</a>！</span></div></div><div class="tag_share"><div class="post-meta__tag-list"><a class="post-meta__tags" href="/tags/PHP/">PHP</a></div><div class="post_share"><div class="social-share" data-image="/images/php.png" data-sites="facebook,twitter,wechat,weibo,qq"></div><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/social-share.js/dist/css/share.min.css" media="print" onload="this.media='all'"><script src="https://cdn.jsdelivr.net/npm/social-share.js/dist/js/social-share.min.js" defer></script></div></div><nav class="pagination-post" id="pagination"><div class="prev-post pull-left"><a href="/2020/08/13/ubuntu-update-source/"><img class="prev-cover" src= "" data-lazy-src="/images/ubuntu.jpg" onerror="onerror=null;src='/img/404.jpg'" alt="cover of previous post"><div class="pagination-info"><div class="label">上一篇</div><div class="prev_info">ubuntu20.04 更新软件源</div></div></a></div><div class="next-post pull-right"><a href="/2020/06/05/composer-exhaust/"><img class="next-cover" src= "" data-lazy-src="/images/code.jpg" onerror="onerror=null;src='/img/404.jpg'" alt="cover of next post"><div class="pagination-info"><div class="label">下一篇</div><div class="next_info">composer require 拓展包时，出现内存溢出</div></div></a></div></nav><div class="relatedPosts"><div class="headline"><i class="fas fa-thumbs-up fa-fw"></i><span> 相关推荐</span></div><div class="relatedPosts-list"><div><a href="/2019/11/09/function1/" title="PHP 返回一维数组中，有序组合成 N 个元素的数组"><img class="cover" src= "" data-lazy-src="/images/420e7cbd02507bcbe39236a3b384a59f17.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2019-11-09</div><div class="title">PHP 返回一维数组中，有序组合成 N 个元素的数组</div></div></a></div><div><a href="/2022/08/15/long-lat-distance/" title="【函数分享】计算两个经纬度之间的距离"><img class="cover" src= "" data-lazy-src="/images/code.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2022-08-15</div><div class="title">【函数分享】计算两个经纬度之间的距离</div></div></a></div><div><a href="/2022/08/15/my-online-log/" title="线上日志系统"><img class="cover" src= "" data-lazy-src="/images/code.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2022-08-15</div><div class="title">线上日志系统</div></div></a></div><div><a href="/2023/05/24/php-aes/" title="前后端接口传输AES加密"><img class="cover" src= "" data-lazy-src="/images/code.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2023-05-24</div><div class="title">前后端接口传输AES加密</div></div></a></div><div><a href="/2019/08/30/php-basic-1/" title="php常量以及数据类型"><img class="cover" src= "" data-lazy-src="/images/php.png" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2019-08-30</div><div class="title">php常量以及数据类型</div></div></a></div><div><a href="/2019/10/25/php-array/" title="php 遍历数组的几种方式"><img class="cover" src= "" data-lazy-src="/images/php.png" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2019-10-25</div><div class="title">php 遍历数组的几种方式</div></div></a></div></div></div></div><div class="aside-content" id="aside-content"><div class="card-widget card-info"><div class="card-info-avatar is-center"><img class="avatar-img" src= "" data-lazy-src="/images/avatar.jpg" onerror="this.onerror=null;this.src='/img/friend_404.gif'" alt="avatar"/><div class="author-info__name">沙屿沫</div><div class="author-info__description">科技始终来源于人性</div></div><div class="card-info-data"><div class="card-info-data-item is-center"><a href="/archives/"><div class="headline">文章</div><div class="length-num">74</div></a></div><div class="card-info-data-item is-center"><a href="/tags/"><div class="headline">标签</div><div class="length-num">42</div></a></div><div class="card-info-data-item is-center"><a href="/categories/"><div class="headline">分类</div><div class="length-num">15</div></a></div></div><a class="button--animated" id="card-info-btn" target="_blank" rel="noopener" href="https://gitee.com/shayvmo"><i class="fab fa-github"></i><span>Follow Me</span></a></div><div class="card-widget card-announcement"><div class="item-headline"><i class="fas fa-bullhorn card-announcement-animation"></i><span>公告</span></div><div class="announcement_content">生活就像海洋，只有意志坚强的人才能到达彼岸。</div></div><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><i class="fas fa-stream"></i><span>目录</span></div><div class="toc-content"><ol class="toc"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E3%80%90%E4%B8%8D%E5%90%91%E5%90%8E%E5%85%BC%E5%AE%B9%E5%8F%98%E6%9B%B4%E3%80%91"><span class="toc-number">1.</span> <span class="toc-text">【不向后兼容变更】</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%BC%82%E5%B8%B8%E5%A4%84%E7%90%86%E5%92%8C%E9%94%99%E8%AF%AF%E7%9A%84%E6%9B%B4%E6%94%B9"><span class="toc-number">1.1.</span> <span class="toc-text">异常处理和错误的更改</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%85%B3%E4%BA%8E%E9%97%B4%E6%8E%A5%E4%BD%BF%E7%94%A8%E5%8F%98%E9%87%8F%E3%80%81%E5%B1%9E%E6%80%A7%E5%92%8C%E6%96%B9%E6%B3%95%E7%9A%84%E5%8F%98%E5%8C%96"><span class="toc-number">1.2.</span> <span class="toc-text">关于间接使用变量、属性和方法的变化</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#list-%E5%A4%84%E7%90%86%E6%96%B9%E5%BC%8F%E5%8F%98%E5%8A%A8"><span class="toc-number">1.3.</span> <span class="toc-text">list() 处理方式变动</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#foreach-%E5%8F%98%E5%8C%96"><span class="toc-number">1.4.</span> <span class="toc-text">foreach 变化</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#HTTP-RAW-POST-DATA-%E8%A2%AB%E7%A7%BB%E9%99%A4"><span class="toc-number">1.5.</span> <span class="toc-text">$HTTP_RAW_POST_DATA 被移除</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E3%80%90%E6%96%B0%E7%89%B9%E6%80%A7%E3%80%91"><span class="toc-number">2.</span> <span class="toc-text">【新特性】</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E6%A0%87%E9%87%8F%E7%B1%BB%E5%9E%8B%E5%A3%B0%E6%98%8E"><span class="toc-number">2.1.</span> <span class="toc-text">标量类型声明</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E8%BF%94%E5%9B%9E%E5%80%BC%E7%B1%BB%E5%9E%8B%E5%A3%B0%E6%98%8E"><span class="toc-number">2.2.</span> <span class="toc-text">返回值类型声明</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#null%E5%90%88%E5%B9%B6%E8%BF%90%E7%AE%97%E7%AC%A6"><span class="toc-number">2.3.</span> <span class="toc-text">null合并运算符</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%A4%AA%E7%A9%BA%E8%88%B9%E6%93%8D%E4%BD%9C%E7%AC%A6%EF%BC%88%E7%BB%84%E5%90%88%E6%AF%94%E8%BE%83%E7%AC%A6%EF%BC%89-lt-gt"><span class="toc-number">2.4.</span> <span class="toc-text">太空船操作符（组合比较符） &lt;&#x3D;&gt;</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E9%80%9A%E8%BF%87define-%E5%AE%9A%E4%B9%89%E5%B8%B8%E9%87%8F%E6%95%B0%E7%BB%84"><span class="toc-number">2.5.</span> <span class="toc-text">通过define()定义常量数组</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%8C%BF%E5%90%8D%E7%B1%BB"><span class="toc-number">2.6.</span> <span class="toc-text">匿名类</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E6%95%B4%E6%95%B0%E9%99%A4%E6%B3%95%E5%87%BD%E6%95%B0intdiv"><span class="toc-number">2.7.</span> <span class="toc-text">整数除法函数intdiv()</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E3%80%90PHP-7-0-x-%E5%BC%83%E7%94%A8%E7%9A%84%E5%8A%9F%E8%83%BD%E3%80%91"><span class="toc-number">3.</span> <span class="toc-text">【PHP 7.0.x 弃用的功能】</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#password-hash-%E7%9B%90%E5%80%BC%E9%80%89%E9%A1%B9"><span class="toc-number">3.1.</span> <span class="toc-text">password_hash() 盐值选项</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%8F%98%E6%9B%B4%E7%9A%84%E5%87%BD%E6%95%B0"><span class="toc-number">3.2.</span> <span class="toc-text">变更的函数</span></a></li></ol></li></ol></div></div><div class="card-widget card-recent-post"><div class="item-headline"><i class="fas fa-history"></i><span>最新文章</span></div><div class="aside-list"><div class="aside-list-item"><a class="thumbnail" href="/2023/06/05/webman-route-annotation/" title="webman 框架 注解路由插件"><img src= "" data-lazy-src="/images/code.jpg" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="webman 框架 注解路由插件"/></a><div class="content"><a class="title" href="/2023/06/05/webman-route-annotation/" title="webman 框架 注解路由插件">webman 框架 注解路由插件</a><time datetime="2023-06-05T07:04:07.000Z" title="发表于 2023-06-05 15:04:07">2023-06-05</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2023/05/29/nginx-reload-not-effect/" title="windows服务器重载nginx配置无效"><img src= "" data-lazy-src="/images/code.jpg" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="windows服务器重载nginx配置无效"/></a><div class="content"><a class="title" href="/2023/05/29/nginx-reload-not-effect/" title="windows服务器重载nginx配置无效">windows服务器重载nginx配置无效</a><time datetime="2023-05-29T08:03:45.000Z" title="发表于 2023-05-29 16:03:45">2023-05-29</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2023/05/24/php-aes/" title="前后端接口传输AES加密"><img src= "" data-lazy-src="/images/code.jpg" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="前后端接口传输AES加密"/></a><div class="content"><a class="title" href="/2023/05/24/php-aes/" title="前后端接口传输AES加密">前后端接口传输AES加密</a><time datetime="2023-05-24T07:00:39.000Z" title="发表于 2023-05-24 15:00:39">2023-05-24</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2022/08/16/yar-demo/" title="Yar拓展，实现PHP的RPC服务"><img src= "" data-lazy-src="/images/code.jpg" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="Yar拓展，实现PHP的RPC服务"/></a><div class="content"><a class="title" href="/2022/08/16/yar-demo/" title="Yar拓展，实现PHP的RPC服务">Yar拓展，实现PHP的RPC服务</a><time datetime="2022-08-16T09:30:43.000Z" title="发表于 2022-08-16 17:30:43">2022-08-16</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2022/08/15/my-online-log/" title="线上日志系统"><img src= "" data-lazy-src="/images/code.jpg" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="线上日志系统"/></a><div class="content"><a class="title" href="/2022/08/15/my-online-log/" title="线上日志系统">线上日志系统</a><time datetime="2022-08-15T09:03:01.000Z" title="发表于 2022-08-15 17:03:01">2022-08-15</time></div></div></div></div></div></div></main><footer id="footer" style="background-image: url('/images/php.png')"><div id="footer-wrap"><div class="copyright">&copy;2019 - 2023 By 沙屿沫</div><div class="framework-info"><span>框架 </span><a target="_blank" rel="noopener" href="https://hexo.io">Hexo</a><span class="footer-separator">|</span><span>主题 </span><a target="_blank" rel="noopener" href="https://github.com/jerryc127/hexo-theme-butterfly">Butterfly</a></div><div class="footer_custom_text"><a href="http://www.beian.miit.gov.cn/" target="_blank" rel="noopener"><img class="icp-icon" src= "" data-lazy-src="/images/icp.png"><span>粤ICP备18133330号</span></a></div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="阅读模式"><i class="fas fa-book-open"></i></button><button id="darkmode" type="button" title="浅色和深色模式转换"><i class="fas fa-adjust"></i></button><button id="hide-aside-btn" type="button" title="单栏和双栏切换"><i class="fas fa-arrows-alt-h"></i></button></div><div id="rightside-config-show"><button id="rightside_config" type="button" title="设置"><i class="fas fa-cog fa-spin"></i></button><button class="close" id="mobile-toc-button" type="button" title="目录"><i class="fas fa-list-ul"></i></button><button id="go-up" type="button" title="回到顶部"><i class="fas fa-arrow-up"></i></button></div></div><div id="local-search"><div class="search-dialog"><div class="search-dialog__title" id="local-search-title">本地搜索</div><div id="local-input-panel"><div id="local-search-input"><div class="local-search-box"><input class="local-search-box--input" placeholder="搜索文章" type="text"/></div></div></div><hr/><div id="local-search-results"></div><span class="search-close-button"><i class="fas fa-times"></i></span></div><div id="search-mask"></div></div><div><script src="/js/utils.js"></script><script src="/js/main.js"></script><script src="https://cdn.jsdelivr.net/npm/vanilla-lazyload/dist/lazyload.iife.min.js"></script><script src="/js/search/local-search.js"></script><div class="js-pjax"></div><script defer="defer" id="ribbon" src="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1/dist/canvas-ribbon.min.js" size="150" alpha="0.6" zIndex="-1" mobile="false" data-click="false"></script><script defer="defer" id="fluttering_ribbon" mobile="false" src="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1/dist/canvas-fluttering-ribbon.min.js"></script><script id="click-show-text" src="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1/dist/click-show-text.min.js" data-mobile="false" data-text="富强,民主,文明,和谐,自由,平等,公正,法治,爱国,敬业,诚信,友善" data-fontsize="15px" data-random="false" async="async"></script><script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script></div></body></html>